import Vue from 'vue';
import VueRouter from 'vue-router';
import routerChilder from './routerChilder.js'
import HomeView from '@/views/home/Home.vue';
import Login from '@/views/login/Login.vue';
import NoPage from '@/views/404.vue';
import store from '../store/store';
import {isPlainObject,isString} from 'lodash';
Vue.use(VueRouter);
const router = new VueRouter({
  mode: 'hash',
  // base: (process.env.NODE_ENV === 'development'?'/':'/'),
  routes: [
    {
      path: '/',
      name: 'home',
      meta: { title: '首页' },
      component: HomeView,
      children:routerChilder,
      redirect:'/work'
    },
    {
      path: '/login',
      name: 'login',
      meta: { title: '登录' },
      component: Login,
    },
    {
      path: '*',
      redirect: '/404',
      component: NoPage,
    }
  ],
});

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}



//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
  document.title = `${to.meta.title}`;
  const userInfo = store.state.app.userInfo;
  const isLogin = userInfo && isPlainObject(userInfo) && userInfo.id ? true : false;
  if (!isLogin && to.path !== '/login') {
      next('/login');
  } else{
      next();
  }
});


export default router;
